package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.SetmealDao;
import com.itheima.service.ReportService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author FCJ
 * @date 2020-01-12 10:16
 */
@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private SetmealDao setmealDao;

    @Override
    public Map<String, Object> getBusinessReport() throws Exception {
        Map<String, Object> map = new HashMap<>();
        Date todydate = new Date();
        String today = DateUtils.parseDate2String(todydate);
        map.put("reportDate", today);
        map.put("todayNewMember", memberDao.findNewMemberCountByDate(today));
        map.put("totalMember", memberDao.findTotalMember());
        HashMap<String, String> thisWeek = new HashMap<>();
        thisWeek.put("mintime", DateUtils.parseDate2String(DateUtils.getThisWeekMonday()));
        thisWeek.put("maxtime", DateUtils.parseDate2String(DateUtils.getNextWeekMonday(todydate)));
        map.put("thisWeekNewMember", memberDao.findNewMember(thisWeek));
        HashMap<String, String> thisMonth = new HashMap<>();
        thisMonth.put("mintime", DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth()));
        thisMonth.put("maxtime", DateUtils.parseDate2String(DateUtils.getFirstDay4NextMonth()));
        map.put("thisMonthNewMember", memberDao.findNewMember(thisMonth));
        map.put("todayOrderNumber", orderDao.findCountByDate(today));
        map.put("todayVisitsNumber", orderDao.findVisitsCountByDate(today));
        map.put("thisWeekOrderNumber", orderDao.findCountBetweenDate(thisWeek));
        map.put("thisWeekVisitsNumber", orderDao.findVisitsCountBetweenDate(thisWeek));
        map.put("thisMonthOrderNumber", orderDao.findCountBetweenDate(thisMonth));
        map.put("thisMonthVisitsNumber", orderDao.findVisitsCountBetweenDate(thisMonth));
        List<Map<String, Object>> hotSetmeals = setmealDao.findSetmealCount();
        Integer totalCount = orderDao.findTotalCount();
        for (Map hotSetmeal : hotSetmeals) {
            long value = (long) hotSetmeal.get("value");
            hotSetmeal.put("setmeal_count", String.valueOf(value));
            hotSetmeal.put("proportion", String.format("%.2f", value * 100.0 / totalCount) + "%");
        }

            map.put("hotSetmeal", hotSetmeals);

            return map;
        }
    }